﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;
//
using PerceptiveMCAPI;
using PerceptiveMCAPI.Types;

namespace PerceptiveMCAPI_Test
{

   public class ApiHelper
   {
      public const string MethodPath = @"App_Data\ApiMethods.xml";

      public static List<ApiMethod> GetApiMethods()
      {
         List<ApiMethod> methods = new List<ApiMethod>();
         HttpContext context = HttpContext.Current;
         string path = context.Request.PhysicalApplicationPath + MethodPath;
         XDocument apiDoc = XDocument.Load( path );
         var m = from elem in apiDoc.Descendants( "method" )
                 select new ApiMethod
                 {
                    name = elem.Attribute( "name" ).Value,
                    title = elem.Attribute( "title" ).Value,
                    section = elem.Attribute( "section" ).Value,
                    panelInput = elem.Attribute( "panelInput" ).Value,
                    panelOutput = elem.Attribute( "panelOutput" ).Value
                 };

         foreach ( ApiMethod method in m )
         {
            methods.Add( method );
         }
         return methods;
      }
      public static ApiMethod GetApiMethodDetail( string methodName )
      {
         ApiMethod method = new ApiMethod();
         HttpContext context = HttpContext.Current;
         string path = context.Request.PhysicalApplicationPath + MethodPath;

         XDocument apiDoc = XDocument.Load( path );
         var m = from elem in apiDoc.Descendants( "method" )
                 where elem.Attribute( "name" ).Value == methodName
                 select new ApiMethod
                 {
                    name = elem.Attribute( "name" ).Value,
                    title = elem.Attribute( "title" ).Value,
                    section = elem.Attribute( "section" ).Value,
                    panelInput = elem.Attribute( "panelInput" ).Value,
                    panelOutput = elem.Attribute( "panelOutput" ).Value
                 };

         if ( m.Count() > 0 )
         { method = m.FirstOrDefault(); }
         return method;
      }

      // check for errors
      public static bool ValidatorErrorsExist( List<Api_ValidatorMessage> messages )
      {
         foreach ( Api_ValidatorMessage message in messages )
         {
            if ( message.MessageLevel >= EnumValues.MessageLevel.Error )
            { return true; }
         }
         return false;
      }

      public static string DictionaryToString( Dictionary<string, string> dict )
      {
         if ( dict == null || dict.Count == 0 ) return string.Empty;

         StringBuilder sb = new StringBuilder();
         foreach ( KeyValuePair<string, string> kv in dict )
         {
            string key = kv.Key;
            string val = kv.Value;

            sb.AppendFormat( "{0}=>{1}, ", key, val );
         }
         string result = sb.ToString( 0, sb.Length - 2 );
         return result;
      }
      public static string DictionaryToString( Dictionary<string, object> dict )
      {
         if ( dict.Count == 0 ) return string.Empty;

         StringBuilder sb = new StringBuilder();
         foreach ( KeyValuePair<string, object> kv in dict )
         {
            string key = kv.Key;
            string val = kv.Value.ToString();

            sb.AppendFormat( "{0}=>{1}, ", key, val );
         }
         string result = sb.ToString( 0, sb.Length - 2 );
         return result;
      }

      public static string DictionaryToStringHtmlStack( Dictionary<string, string> dict )
      {
         if ( dict.Count == 0 ) return string.Empty;

         StringBuilder sb = new StringBuilder();
         foreach ( KeyValuePair<string, string> kv in dict )
         {
            string key = kv.Key;
            string val = kv.Value;

            sb.AppendFormat( "{0}=>{1}<br />", key, val );
         }
         string result = sb.ToString( 0, sb.Length - 6 );
         return result;
      }
      public static string SortedDictionaryToStringHtmlStack( SortedDictionary<string, string> sDict )
      {
         if ( sDict.Count == 0 ) return string.Empty;

         StringBuilder sb = new StringBuilder();
         foreach ( KeyValuePair<string, string> kv in sDict )
         {
            string key = kv.Key;
            string val = kv.Value;

            sb.AppendFormat( "{0}=>{1}<br />", key, val );
         }
         string result = sb.ToString( 0, sb.Length - 6 );
         return result;
      }
      public static string MakeStringFromList( List<string> list )
      {
         return MakeStringFromList( list, ", " );
      }
      public static string MakeStringFromList( List<string> list, string sep )
      {
         if ( list.Count == 0 ) return string.Empty;

         StringBuilder sb = new StringBuilder();
         foreach ( string entry in list )
         {
            sb.AppendFormat( "{0}{1}", entry, sep );
         }
         return sb.ToString( 0, sb.Length - sep.Length );
      }
      public static List<string> MakeListFromString( string string_value )
      {
         List<string> new_list = new List<string>();
         string[] delim = { ",", ";", "|", " " };
         string[] string_array = string_value.Split( delim, StringSplitOptions.RemoveEmptyEntries );
         foreach ( string entry in string_array )
         {
            new_list.Add( entry );
         }
         return new_list;
      }

      public static string MakeStringHtmlStackFromList( List<string> list )
      {
         if ( list.Count == 0 ) return string.Empty;

         StringBuilder sb = new StringBuilder();
         foreach ( string entry in list )
         {
            sb.AppendFormat( "{0}<br />", entry );
         }
         return sb.ToString( 0, sb.Length - 6 );
      }

      // ===========================================
      public static bool IsNumeric( object Expression )
      {
         bool isNum; double retNum;

         isNum = Double.TryParse( Convert.ToString( Expression ), System.Globalization.NumberStyles.Any,
             System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );

         return isNum;
      }
      public static string FormatApiDateParm( DateTime date )
      {
         return date.ToString( "yyyy-MM-dd HH:mm:ss" );
      }

      public static string GetShortMonthDesc( int month )
      {
         string[] mths = { "BAD", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
         return mths[month].ToString();
      }
      public static string stringNameFML( string fname, string lname )
      {
         return stringNameFML( fname, "", lname );
      }
      public static string stringNameFML( string fname, string mname, string lname )
      {
         StringBuilder sb = new StringBuilder();
         if ( !string.IsNullOrEmpty( fname ) )
         {
            sb.Append( fname + " " );
         }
         if ( !string.IsNullOrEmpty( mname ) )
         {
            sb.Append( fname + " " );
         }
         if ( !string.IsNullOrEmpty( lname ) )
         {
            sb.Append( lname );
         }
         return sb.ToString();
      }
      public static string stringAddress(
         string address1,
         string address2,
         string city,
         string state,
         string postal,
         string country
         )
      {
         StringBuilder sb = new StringBuilder();
         if ( !string.IsNullOrEmpty( address1 ) )
         {
            sb.AppendFormat( "{0}<br />", address1 );
         }
         if ( !string.IsNullOrEmpty( address2 ) )
         {
            sb.AppendFormat( "{0}<br />", address2 );
         }
         if ( !string.IsNullOrEmpty( city ) )
         {
            sb.AppendFormat( "{0}, ", city );
         }
         if ( !string.IsNullOrEmpty( state ) )
         {
            sb.AppendFormat( "{0} ", state );
         }
         if ( !string.IsNullOrEmpty( postal ) )
         {
            sb.AppendFormat( "{0}<br />", postal );
         }
         if ( !string.IsNullOrEmpty( country ) )
         {
            sb.AppendFormat( "{0}", country );
         }
         //
         return sb.ToString();
      }

      // ===========================================================
      public static T GetEnumFromString<T>( string value )
      {
         T result = (T)Enum.Parse( typeof( T ), value );
         return result;
      }
      public static Dictionary<string, int> GetEnumValues<T>()
      {
         Dictionary<string, int> values = new Dictionary<string, int>();

         string[] names = Enum.GetNames( typeof( T ) );
         foreach ( string name in names )
         {
            int val = Convert.ToInt32( ApiHelper.GetEnumFromString<T>( name ) );
            values.Add( name, val );
         }
         return values;
      }

   }
}
